//
//  DateSelectView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 16.03.25.
//

import SwiftUI

struct DateSelectView: View {
    
    @Binding var date: Date
    
    var dateType: DateSelectType
    
    var isFullWidth = false
    
    var leftClick:(Date)->Void
    
    var rightClick:(Date)->Void
    
    var body: some View {
        HStack (spacing: 0){
            Button(action: {
                switch dateType {
                case .week:
                    date = date.previousWeek()
                case .month:
                    date = date.previousMonth()
                case .year:
                    date = date.previousYear()
                }
                leftClick(date)
            }) {
                ZStack {
                    Color.koiColorBackgroundItem
                        .frame(width: 28,height: 28)
                        .cornerRadius(50)
                    Image("koi_ic_arrow_left")
                }
            }.buttonStyle(PlainButtonStyle())
            
            if (isFullWidth) {
                Spacer()
            }
            Text(getDateText())
                .font(.system(size: 14))
                .fontWeight(.semibold)
                .foregroundColor(Color.koiColorTextMain)
                .padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
            if (isFullWidth) {
                Spacer()
            }
            Button(action: {
                switch dateType {
                case .week:
                    date = date.nextWeek()
                case .month:
                    date = date.nextMonth()
                case .year:
                    date = date.nextYear()
                }
                rightClick(date)
            }) {
                ZStack {
                    Color.koiColorBackgroundItem
                        .frame(width: 28,height: 28)
                        .cornerRadius(50)
                    Image("koi_ic_arrow_right")
                }
            }.buttonStyle(PlainButtonStyle())
        }
    }
    
    private func getDateText() -> String {
        switch dateType {
        case .week:
            return date.toWeek()
        case .month:
            return date.toYearMonth()
        case .year:
            return date.toYear()
        }
    }
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var date = Date()
    DateSelectView(date:$date, dateType: .week, leftClick: { _ in }, rightClick: { _ in })
}
